﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LeoShi.Soft.OpenSinaAPI;
using System.IO;
using System.Xml;
using LTP.Common;

namespace TCS.Web.MembersHome.WeiBoManager
{
    public partial class WeiboMine : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        void BindData()
        {
            updateMyWeibo();
        }
        private void updateMyWeibo()
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string url = "http://api.t.sina.com.cn/statuses/friends_timeline.json";
            string ret = httpRequest.Request(url, string.Empty);

            System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;

            List<LTP.Common.Status> listObj = JSONConvert.DeserializeJsonToListObject(ret);
            //DataSet dsret = new DataSet();
            //dsret = ConvertXMLToDataSet(ret);
            TCS.Model.Weibo.Cache_WeiboInfor model = new TCS.Model.Weibo.Cache_WeiboInfor();
            TCS.BLL.Weibo.Cache_WeiboInfor bll = new TCS.BLL.Weibo.Cache_WeiboInfor();

            TCS.Model.Weibo.WeiBo_CacheUserInfo modeluser = new TCS.Model.Weibo.WeiBo_CacheUserInfo();
            TCS.BLL.Weibo.WeiBo_CacheUserInfo bllUser = new TCS.BLL.Weibo.WeiBo_CacheUserInfo();

            TCS.Model.Weibo.WeiBo_CacheForward modelretweeted = new TCS.Model.Weibo.WeiBo_CacheForward();
            TCS.BLL.Weibo.WeiBo_CacheForward bllret = new TCS.BLL.Weibo.WeiBo_CacheForward();
            if (listObj!=null)
            {

                for (int i = 0; i < listObj.Count; i++)
                {
                    model.Member_ID = int.Parse(Session["memberid"].ToString());
                    model.WB_ID =long.Parse(listObj[i].id);
                    model.WB_Status = listObj[i].text;
                    model.Truncated = listObj[i].truncated;
                    model.Thumbnail_Pic = listObj[i].thumbnail_pic;
                    model.State = 0;
                    model.Source = listObj[i].source;
                    model.Picture = listObj[i].original_pic;
                    model.IsOriginal = 0;
                    model.InfoType = 1;
                    model.Cache_time = System.DateTime.Now;
                    model.Cache_Type = 1;
                    model.CreateTime = DateTime.ParseExact(listObj[i].created_at, "ddd MMM dd HH:mm:ss zzz yyyy",provider);
                    bll.Add(model);
                    if (listObj[i].user != null)
                    {
                        modeluser.CacheWb_ID = model.WB_ID;
                        modeluser.CityID = int.Parse(listObj[i].user.city);
                        modeluser.Create_ai = DateTime.ParseExact(listObj[i].user.created_at, "ddd MMM dd HH:mm:ss zzz yyyy", provider);
                        modeluser.Description = listObj[i].user.description;
                        modeluser.Domain = listObj[i].user.domain;
                        modeluser.Favourites_count =int.Parse( listObj[i].user.favourites_count);
                        modeluser.Followers_count =int.Parse( listObj[i].user.followers_count);
                        modeluser.Following = bool.Parse(listObj[i].user.following);
                        modeluser.Friends_count =int.Parse( listObj[i].user.friends_count);
                        modeluser.Gender = listObj[i].user.gender;
                        modeluser.Location = listObj[i].user.location;
                        modeluser.Name = listObj[i].user.name;
                        modeluser.Profile_Image_Url = listObj[i].user.profile_image_url;
                        modeluser.ProvinceID = int.Parse(listObj[i].user.province);
                        modeluser.Screen_Name = listObj[i].user.screen_name;
                        modeluser.Statuses_count = int.Parse(listObj[i].user.statuses_count);
                        modeluser.Url = listObj[i].user.url;
                        modeluser.UserID =Convert.ToInt32( model.Member_ID);
                        modeluser.Verified =bool.Parse( listObj[i].user.verified);
                        bllUser.Add(modeluser);
                    }
                    if (listObj[i].retweeted_status != null)
                    {
                        modelretweeted.CacheWb_ID = model.WB_ID;
                        modelretweeted.Status_id =long.Parse( listObj[i].retweeted_status.id);
                        modelretweeted.Content = listObj[i].retweeted_status.text;
                        modelretweeted.Original_pic = listObj[i].retweeted_status.original_pic;
                        modelretweeted.Bmiddle_pic = listObj[i].retweeted_status.bmiddle_pic;
                        modelretweeted.Thumbnail_pic = listObj[i].retweeted_status.thumbnail_pic;
                        bllret.Add(modelretweeted);
                        if (listObj[i].retweeted_status.user != null)
                        {
                            modeluser.CacheWb_ID =long.Parse( listObj[i].retweeted_status.id);
                            modeluser.CityID = int.Parse(listObj[i].retweeted_status.user.city);
                            modeluser.Create_ai = DateTime.ParseExact(listObj[i].retweeted_status.user.created_at, "ddd MMM dd HH:mm:ss zzz yyyy", provider);
                            modeluser.Description = listObj[i].retweeted_status.user.description;
                            modeluser.Domain = listObj[i].retweeted_status.user.domain;
                            modeluser.Favourites_count =int.Parse( listObj[i].retweeted_status.user.favourites_count);
                            modeluser.Followers_count =int.Parse( listObj[i].retweeted_status.user.followers_count);
                            modeluser.Following = bool.Parse(listObj[i].retweeted_status.user.following);
                            modeluser.Friends_count =int.Parse( listObj[i].retweeted_status.user.friends_count);
                            modeluser.Gender = listObj[i].retweeted_status.user.gender;
                            modeluser.Location = listObj[i].retweeted_status.user.location;
                            modeluser.Name = listObj[i].retweeted_status.user.name;
                            modeluser.Profile_Image_Url = listObj[i].retweeted_status.user.profile_image_url;
                            modeluser.ProvinceID =int.Parse( listObj[i].retweeted_status.user.province);
                            modeluser.Screen_Name = listObj[i].retweeted_status.user.screen_name;
                            modeluser.Statuses_count = int.Parse(listObj[i].retweeted_status.user.statuses_count);
                            modeluser.Url = listObj[i].retweeted_status.user.url;
                            modeluser.UserID =Convert.ToInt32( model.Member_ID);
                            modeluser.Verified =bool.Parse( listObj[i].retweeted_status.user.verified);
                            bllUser.Add(modeluser);
                        }
                    }
                }


            }
            //BindData();
            string a = "";
        }
    }
}
